<?php
/**
 * mpPackager processor.
 *
 * @category    Development
 * @package 	mpPackager
 * @subpackage  Processor
 * @author		Murat Purc <murat@purc.de>
 * @copyright   Copyright (c) 2009-2010 Murat Purc (http://www.purc.de)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
 * @version     $Id: Processor.php 2 2010-04-11 18:09:46Z purcaholic $
 */


/**
 * Packager processor...
 *
 * @category    Development
 * @package 	mpPackager
 * @subpackage  Processor
 * @author		Murat Purc <murat@purc.de>
 */
class mpPackager_Processor
{

    /**
     * Configuration instance
     * @var  mpPackager_Config
     */
    private $_config;

    /**
     * Packager instance
     * @var  mpPackager
     */
    private $_packager;


    /**
     * Constructor
     */
    public function __construct()
    {
    }


    /**
     * Configuration instance setter.
     *
     * @param   mpPackager_Config  $cfg
     * @return  void
     */
    public function setConfig(mpPackager_Config $cfg)
    {
        $this->_config = $cfg;
    }


    /**
     * Packager instance setter.
     *
     * @param   mpPackager  $packager
     * @return  void
     */
    public function setPackager(mpPackager $packager)
    {
        $this->_packager = $packager;
    }


    /**
     * The main method to process package generation.
     *
     * @return  void
     */
    public function run()
    {
        if (!$this->_config instanceof mpPackager_Config) {
            throw new mpPackagerException('Missing mpPackager_Config instance');
        } elseif (!$this->_packager instanceof mpPackager) {
            throw new mpPackagerException('Missing mpPackager instance');
        }

        // get package list and source dir
        $packageList = $this->_config->get('packageList');
        $source      = $this->_config->get('source');
        $destination = $this->_config->get('destination');

        // process all items
        foreach ($packageList as $item) {
            $item = trim($item);
            if ($item == '' || strlen($item) == 1) {
                continue;
            }

            $action = substr($item, 0, 1);
            $item   = substr($item, 1);

            if ($action == '+') {
                $fileInfo = new mpPackager_FileInfo($source . $item);
                if ($fileInfo->isFile()) {
                    // copy file
                    $this->_packager->copyFile($item);
                } elseif ($fileInfo->isDir()) {
                    // copy directory
                    $this->_packager->copyDir($item, $item);
                }
            } elseif ($action == '-') {
                // delete from destination
                $this->_packager->deleteDestination($item);
            }
        }
    }

}
